#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.common.bean.vo;

import ${package}.common.emum.DadyEnum;
import ${package}.common.emum.ReturnCodeEnum;
import com.google.common.base.MoreObjects;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;


/**
 * Description: 统一返回实例
 *
 * @author wupanhua
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Data
@NoArgsConstructor
public class JsonResult<T> implements Serializable {

   private static final long serialVersionUID = 1L;

   /**
    * 返回代号
    */
   @ApiModelProperty("返回代号")
   private Integer code;
   /**
    *返回信息
    */
   @ApiModelProperty("返回信息")
   private String msg;
   /**
    *返回数据
    */
   @ApiModelProperty("返回数据")
   private T data;



   /**
    * Description:
    * <实例一个JsonResult>
    * @author wupanhua
    * @date 13:43 2019/8/8
    * @param code 1
    * @param msg 2
    * @param data 3
    * @return JsonResult
    **/
   public static <T> JsonResult<T> getInstant(Integer code, String msg, T data) {
      return new JsonResult<>(code, msg, data);
   }

   /**
    * Description:
    * <实例个JsonResult>
    * @author wupanhua
    * @date 13:43 2019/8/8
    * @param dadyEnum 1
    * @param data 2
    * @return JsonResult
    **/
   public static <T> JsonResult<T> getInstant(DadyEnum dadyEnum, T data) {
      return new JsonResult<>(dadyEnum.getCode(), dadyEnum.getMsg(), data);
   }

   /**
    * Description:
    * <实例一个JsonResult>
    * @author wupanhua
    * @date 13:44 2019/8/8
    * @param code 1
    * @param msg 2
    * @return JsonResult
    **/
   public static <T> JsonResult<T> getInstant(Integer code, String msg) {
      return new JsonResult<>(code,msg);
   }

   /**
    * Description:
    * <实例一个JsonResult>
    * @author wupanhua
    * @date 13:44 2019/8/8
    * @param dady 1
    * @return JsonResult
    **/
   public static <T> JsonResult<T> getInstant(DadyEnum dady) {
      return new JsonResult<>(dady.getCode(), dady.getMsg());
   }

   private JsonResult(Integer code, String msg, T data) {
      this.code = code;
      this.msg = msg;
      this.data = data;
   }

   private JsonResult(Integer code, String msg) {
      this.code = code;
      this.msg = msg;
   }

   @Override
   public String toString() {
      return MoreObjects.toStringHelper(this)
              .add("code", code)
              .add("msg", msg)
              .add("data", data)
              .toString();
   }

   /**
    * 提取JsonResult数据
    * @param jsonResult        JsonResult数据
    * @param nullValue         默认返回
    * @author wenxiaopeng
    * @date 2021/07/04 18:24
    * @return T
    **/
   public static <T> T getOrDefault(JsonResult<T> jsonResult, T nullValue) {
      if(null!=jsonResult && ReturnCodeEnum.SUCCESS.getCode().equals(jsonResult.getCode())) {
         return jsonResult.getData();
      }
      return nullValue;
   }

   /**
    * 快速返回成功
    *
    * @param data 附带的数据
    * @return ${package}.common.bean.vo.JsonResult<T>
    * @author wenxiaopeng
    * @date 2021/07/13 18:09
    **/
   public static <T> JsonResult<T> success(T data) {
      return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, data);
   }

   /**
    * 快速返回成功
    *
    * @return ${package}.common.bean.vo.JsonResult<T>
    * @author wenxiaopeng
    * @date 2021/07/13 18:09
    **/
   public static JsonResult success() {
      return JsonResult.getInstant(ReturnCodeEnum.SUCCESS);
   }

   public static JsonResult<String> error(Throwable e) {
      String cause = null;
      if (null != e) {
         StringWriter writer = new StringWriter();
         PrintWriter printWriter = new PrintWriter(writer);
         e.getCause().printStackTrace(printWriter);
         printWriter.flush();
         cause = writer.toString();
      }
      return JsonResult.getInstant(ReturnCodeEnum.ERROR, cause);
   }
}
